#include <vector>
#include <set>
#include <iostream>

using namespace std;

class Solution {
public:
    bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
        multiset<int64_t> window;
        if (k + 1 > nums.size()) {
            for (int item : nums) {
                int64_t sub = INT64_MAX;
                multiset<int64_t>::iterator it = window.insert(item);
                if (it != window.begin()) {
                    it--;
                    sub = min(item - *it, sub);
                    it++;
                }
                it++;
                if (it != window.end()) {
                    sub = min(*it - item, sub);
                }
                if (sub <= t) {
                    return true;
                }
            }
            return false;
        }
        for (int i = 0; i <= k; i++) {
            int64_t sub = INT64_MAX;
            multiset<int64_t>::iterator it = window.insert(nums[i]);
            if (it != window.begin()) {
                it--;
                sub = min(nums[i] - *it, sub);
                it++;
            }
            it++;
            if (it != window.end()) {
                sub = min(*it - nums[i], sub);
            }
            if (sub <= t) {
                return true;
            }
        }
        for (int i = k + 1; i < nums.size(); i++) {
            window.erase(window.find(nums[i - k - 1]));
            int64_t sub = INT64_MAX;
            multiset<int64_t>::iterator it = window.insert(nums[i]);
            if (it != window.begin()) {
                it--;
                sub = min(nums[i] - *it, sub);
                it++;
            }
            it++;
            if (it != window.end()) {
                sub = min(*it - nums[i], sub);
            }
            if (sub <= t) {
                return true;
            }
        }
        return false;
    }
};

int main() {
    vector<int> input = {-1, 2147483647};
    Solution solution;
    bool output = solution.containsNearbyAlmostDuplicate(input, 1, 2147483647);
    cout << output << endl;
    return 0;
}